from datetime import datetime
from common.models import db


class CourseTag(db.Model):
    """课程标签"""
    __tablename__ = "tb_course_tag"
    id = db.Column(db.Integer, primary_key=True,autoincrement=True,doc='标签的id')
    tag_name = db.Column(db.String(32), doc='标签名称')
    sequence = db.Column(db.Integer, doc="展示顺序")
    course = db.relationship('Course',secondary='courseandtag',backref=db.backref('course_tag'))


class CourseandTag(db.Model):
    """课程表和标签表的中间表"""
    __tablename__ = "courseandtag"
    id = db.Column(db.Integer,primary_key=True)
    course_id = db.Column(db.Integer, db.ForeignKey('tb_course.id', ondelete='CASCADE'),doc="课程的主键")
    tag_id = db.Column(db.Integer, db.ForeignKey('tb_course_tag.id', ondelete='CASCADE'),doc="课程标签的主键")


class Course(db.Model):
    """课程表"""
    __tablename__ = "tb_course"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc='课程id')
    course_name = db.Column(db.String(32), doc='课程名')
    course_status = db.Column(db.Integer,default=1,  doc='0即将上线1已上线')
    course_img = db.Column(db.String(256), doc='课程封面')
    course_introduce = db.Column(db.String(256), doc='课程介绍')
    learner = db.Column(db.Integer,default=0,  doc='学习人数')
    course_type = db.Column(db.String(32),doc='课程类别')
    attention = db.Column(db.Integer,default=0,  doc='关注人数')
    chapter = db.relationship('Chapters', backref='chapter_course')
    comment = db.relationship('Comments', backref='comment_course')
    course_time = db.Column(db.DateTime,nullable=True,doc="课程时间")
    is_attention = db.Column(db.Integer,default=0,doc="0关注1不关注")


class Chapters(db.Model):
    """课程的章节"""
    __tablename__ = "tb_chapter"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc='章节id')
    title = db.Column(db.String(32), doc='章节名')
    serial_num = db.Column(db.Integer, doc="展示顺序")
    course = db.Column(db.Integer, db.ForeignKey('tb_course.id', ondelete='CASCADE'),doc='课程')
    sections = db.relationship('Sections',backref='section_chapter')


class Sections(db.Model):
    """章节的小节"""
    __tablename__ = "tb_section"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc='小节id')
    section_title = db.Column(db.String(32), doc='小节名')
    serial_num = db.Column(db.Integer, doc="展示顺序")
    chapters = db.Column(db.Integer, db.ForeignKey('tb_chapter.id', ondelete='CASCADE'), doc='章节')
    learn_time = db.Column(db.Float, default=0.00, doc="视频时长")
    video_url= db.Column(db.String(256),nullable=True, doc='视频地址')
    like_count = db.Column(db.Integer, default=0, doc="点赞次数")


class Comments(db.Model):
    """课程评论"""
    __tablename__ = 'tb_comment'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc='评论id')
    user = db.Column(db.Integer, db.ForeignKey('tb_users.uid',ondelete='CASCADE'),doc='评论人')
    comment_time = db.Column(db.DateTime, default=datetime.now(), doc="评论时间")
    comment_content = db.Column(db.Text, doc='评论内容')
    zan_num = db.Column(db.Integer,default=0, doc="点赞次数")
    parent_id = db.Column(db.Integer, db.ForeignKey('tb_comment.id'), nullable=True, doc="被评论的id")
    course = db.Column(db.Integer, db.ForeignKey('tb_course.id'),doc="课程的id")
    reply_count = db.Column(db.Integer, default=0, doc='回复数')
    is_top = db.Column(db.Boolean, default=False, doc='是否置顶')

class CourseCollection(db.Model):
    """课程关注表"""
    __tablename__ = "tb_colletion"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc='关注id')
    user = db.Column(db.Integer, db.ForeignKey('tb_users.uid', ondelete='CASCADE'), doc='关注人')
    course = db.Column(db.Integer, db.ForeignKey('tb_course.id'), doc="课程的id")

